package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.hitqz.robot.api.business.entity.RobotWarningRecordEntity;
import com.hitqz.robot.api.business.vo.HomeErrorStaticDto;
import com.hitqz.robot.api.business.vo.RobotWarningRecordVo;
import com.hitqz.robot.biz.service.RobotWarningRecordService;
import com.hitqz.robot.common.core.util.R;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 报警记录
 *
 * @author wengyongxin
 * @date 2024-05-12 03:21:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/warning" )
@Tag(description = "warning" , name = "报警管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RobotWarningRecordController {

    private final RobotWarningRecordService robotWarningRecordService;


    /**
     * 分页查询
     * @param page 分页对象
     * @param robotWarningRecord 报警记录
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
//    @PreAuthorize("@pms.hasPermission('warning_view')" )
    public R<Page<RobotWarningRecordVo>> getRobotWarningRecordPage(@ParameterObject Page page, @ParameterObject RobotWarningRecordEntity robotWarningRecord) {
        LambdaQueryWrapper<RobotWarningRecordEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(StrUtil.isNotBlank(robotWarningRecord.getType()),RobotWarningRecordEntity::getType,robotWarningRecord.getType());
		wrapper.like(StrUtil.isNotBlank(robotWarningRecord.getTitle()),
                RobotWarningRecordEntity::getTitle,robotWarningRecord.getTitle());
		wrapper.eq(Objects.nonNull(robotWarningRecord.getTime()),
                RobotWarningRecordEntity::getTime,robotWarningRecord.getTime());
		wrapper.eq(Objects.nonNull(robotWarningRecord.getLevel()),
                RobotWarningRecordEntity::getLevel,robotWarningRecord.getLevel());
        wrapper.orderByDesc(RobotWarningRecordEntity::getId);
        wrapper.orderByDesc(RobotWarningRecordEntity::getTime);
        Page<RobotWarningRecordEntity> robotWarningRecordEntityPage = robotWarningRecordService.page(page, wrapper);
        Page<RobotWarningRecordVo> voPage = new Page<>();
        BeanUtil.copyProperties(robotWarningRecordEntityPage, voPage, false);

        List<RobotWarningRecordVo> vos = Lists.newArrayList();

        for (RobotWarningRecordEntity record: robotWarningRecordEntityPage.getRecords()) {
            RobotWarningRecordVo vo = new RobotWarningRecordVo();
            BeanUtil.copyProperties(record, vo, false);
            vos.add(vo);
        }
        voPage.setRecords(vos);
        return R.ok(voPage);
    }


    /**
     * 通过id查询报警记录
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
//    @PreAuthorize("@pms.hasPermission('warning_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(robotWarningRecordService.getById(id));
    }

    /**
     * 新增报警记录
     * @param robotWarningRecord 报警记录
     * @return R
     */
    @Operation(summary = "新增报警记录" , description = "新增报警记录" )
//    @SysLog("新增报警记录" )
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('warning_add')" )
    public R save(@RequestBody RobotWarningRecordEntity robotWarningRecord) {
        return R.ok(robotWarningRecordService.save(robotWarningRecord));
    }

    /**
     * 修改报警记录
     * @param robotWarningRecord 报警记录
     * @return R
     */
    @Operation(summary = "修改报警记录" , description = "修改报警记录" )
//    @SysLog("修改报警记录" )
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('warning_edit')" )
    public R updateById(@RequestBody RobotWarningRecordEntity robotWarningRecord) {
        return R.ok(robotWarningRecordService.updateById(robotWarningRecord));
    }

    /**
     * 通过id删除报警记录
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除报警记录" , description = "通过id删除报警记录" )
//    @SysLog("通过id删除报警记录" )
    @DeleteMapping
//    @PreAuthorize("@pms.hasPermission('warning_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(robotWarningRecordService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param robotWarningRecord 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
//    @PreAuthorize("@pms.hasPermission('warning_export')" )
    public List<RobotWarningRecordEntity> export(RobotWarningRecordEntity robotWarningRecord,Long[] ids) {
        return robotWarningRecordService.list(Wrappers.lambdaQuery(robotWarningRecord).in(ArrayUtil.isNotEmpty(ids), RobotWarningRecordEntity::getId, ids));
    }


	@GetMapping(value = "read/{id}")
	@Operation(summary = "某条告警已经读取")
	public R read(@PathVariable Integer id) {
		RobotWarningRecordEntity sysWarning = robotWarningRecordService.getById(id);
		if (sysWarning == null) {
			return R.failed("data not found");
		}
		sysWarning.setFlag("1");
		robotWarningRecordService.updateById(sysWarning);
		return R.ok();
	}


	@GetMapping(value = "readAll")
	@Operation(summary = "全部告警读取")
	public R readAll() {
		robotWarningRecordService.update(Wrappers.<RobotWarningRecordEntity>lambdaUpdate()
				.set(RobotWarningRecordEntity::getFlag, "1")
				.eq(RobotWarningRecordEntity::getFlag, "0"));
		return R.ok();
	}

    @GetMapping(value = "getTypeStatistics")
    @Operation(summary = "获取报警类型统计")
    public R<List<HomeErrorStaticDto>> getWarningCount() {
        List<HomeErrorStaticDto> list = org.apache.commons.compress.utils.Lists.newArrayList();

        HomeErrorStaticDto errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto.setNumber(21);
        errorStaticDto.setLabel("测试数据1");
        errorStaticDto.setPercent("14.48");
        list.add(errorStaticDto);


        errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto.setNumber(34);
        errorStaticDto.setLabel("测试数据2");
        errorStaticDto.setPercent("14.48");
        list.add(errorStaticDto);

        return R.ok(list);
    }



    @GetMapping(value = "getLevelStatistics")
    @Operation(summary = "获取报警级别统计")
    public R<List<HomeErrorStaticDto>> getLevelStatistics() {
        List<HomeErrorStaticDto> list = org.apache.commons.compress.utils.Lists.newArrayList();

        HomeErrorStaticDto errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto.setNumber(21);
        errorStaticDto.setLabel("一级");
        errorStaticDto.setPercent("14.48");
        list.add(errorStaticDto);


        errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto.setNumber(34);
        errorStaticDto.setLabel("二级");
        errorStaticDto.setPercent("14.48");
        list.add(errorStaticDto);

        return R.ok(list);
    }


    @GetMapping(value = "getStatusStatistics")
    @Operation(summary = "获取报警状态统计")
    public R<List<HomeErrorStaticDto>> getStatusStatistics() {
        List<HomeErrorStaticDto> list = org.apache.commons.compress.utils.Lists.newArrayList();

        HomeErrorStaticDto errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto.setNumber(21);
        errorStaticDto.setLabel("已读");
        errorStaticDto.setPercent("14.48");
        list.add(errorStaticDto);


        errorStaticDto = new HomeErrorStaticDto();
        errorStaticDto.setNumber(34);
        errorStaticDto.setLabel("未读");
        errorStaticDto.setPercent("14.48");
        list.add(errorStaticDto);

        return R.ok(list);
    }




}